-
Notifications
You must be signed in to change notification settings - Fork 249
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Creation of test functions for contract near-contract-standards #1229
base: master
Are you sure you want to change the base?
Creation of test functions for contract near-contract-standards #1229
Conversation
…:core and creation of test "test_fungible_token_core" in examples/fungible-token
@fabrobles92 It is somewhat right direction, but I want to point out that |
- Added test test_transfer_call_ok in FungibleTokenCore trait - Added test for FungibleTokenMetadataProvider trait
let result = contract.ft_transfer_call(accounts(1), amount.into(), None, "".to_string()); | ||
match result { | ||
PromiseOrValue::Promise(_promise) => { | ||
println!("Handle promise here"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@frol I am having a hard time here trying to get the data from the Promise, I read documentation on how to resolve promises, and they need a callback, the problem is that I dont know if I should make the call back function in here or where I am calling this function.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You won't be able to read the content of _promise
, instead, use near_sdk::test_utils::get_created_receipts
@frol Hello, pr updated, I added a tests module inside core and metadata to group easier the tests and to avoid linting warning if I only put the test behind the feature flag. Also I made a comment in the code about how to interact with the promise of ft_transfer_call. Thanks in advance :D |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@fabrobles92 Thank you for your patience. I have provided more feedback and answered your question
assert_eq!(metadata.spec, FT_METADATA_SPEC.to_string()); | ||
assert_eq!(metadata.name, "Example NEAR fungible token"); | ||
assert_eq!(metadata.symbol, "EXAMPLE".to_string()); | ||
assert_eq!(metadata.icon, Some("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 288 288'%3E%3Cg id='l' data-name='l'%3E%3Cpath d='M187.58,79.81l-30.1,44.69a3.2,3.2,0,0,0,4.75,4.2L191.86,103a1.2,1.2,0,0,1,2,.91v80.46a1.2,1.2,0,0,1-2.12.77L102.18,77.93A15.35,15.35,0,0,0,90.47,72.5H87.34A15.34,15.34,0,0,0,72,87.84V201.16A15.34,15.34,0,0,0,87.34,216.5h0a15.35,15.35,0,0,0,13.08-7.31l30.1-44.69a3.2,3.2,0,0,0-4.75-4.2L96.14,186a1.2,1.2,0,0,1-2-.91V104.61a1.2,1.2,0,0,1,2.12-.77l89.55,107.23a15.35,15.35,0,0,0,11.71,5.43h3.13A15.34,15.34,0,0,0,216,201.16V87.84A15.34,15.34,0,0,0,200.66,72.5h0A15.35,15.35,0,0,0,187.58,79.81Z'/%3E%3C/g%3E%3C/svg%3E".to_string())); | ||
assert_eq!(metadata.reference, None); | ||
assert_eq!(metadata.reference_hash, None); | ||
assert_eq!(metadata.decimals, 24); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test should only validate the structure and logic. The contract is not required to hold specific metadata values. In the case of metadata, we may just test that calling the method does not crash, so just calling it will be enough and the rest is handled by Rust (we already know that the contract implements FungibleTokenMetadataProvider
, and as such we know that the returned value is standard-compliant.
test_transfer_ok(contract); | ||
test_transfer_call_ok(contract); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We also need negative tests, such as:
- Attempt to transfer more tokens that sender account has on its balance - should fail and balance remain the same for both accounts
- Attempt to transfer to a non-registered account - should fail since storage cost is not covered
- Attempt to transfer from a non-registered account
let result = contract.ft_transfer_call(accounts(1), amount.into(), None, "".to_string()); | ||
match result { | ||
PromiseOrValue::Promise(_promise) => { | ||
println!("Handle promise here"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You won't be able to read the content of _promise
, instead, use near_sdk::test_utils::get_created_receipts
@frol
Opening a draft PR to get early feedback, if any; just wanted to make sure I am following the expected structure :)
Thanks in advance!